// HelloWiimedia.cpp
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
	unsigned char	WriteBuffer[MAXREPORTSIZE], In[MAXREPORTSIZE];

	DWORD written;
	bool One, Two, A, B, Minus, Plus, Home, Up, Down, Left, Right;

	wm_base w;
	if (w.Init()) {
		printf("Init success");
		WriteBuffer[0]=0x12;
		WriteBuffer[1]=0x00;
		WriteBuffer[2]=0x31;
		WriteFile( w.hWiiRemoteHID, WriteBuffer ,w.Caps.OutputReportByteLength,&written,NULL);
		printf("caps[O:%d,I:%d]\n",w.Caps.OutputReportByteLength,w.Caps.InputReportByteLength);
		printf("written=%d\n",written);
		ReadFile(  w.hWiiRemoteHID, In, w.Caps.InputReportByteLength, &written, NULL );
		printf("received=%d\n",written);
		
		while (true) {
			ReadFile(  w.hWiiRemoteHID, In, w.Caps.InputReportByteLength, &written, NULL );
			printf("x=%4d y=%4d z=%4d\n",In[3],In[4],In[5]);

			Left=  In[1] & 0x0001 ? true : false ;
			Right= In[1] & 0x0002 ? true : false ;
			Down=  In[1] & 0x0004 ? true : false ;
			Up  =  In[1] & 0x0008 ? true : false ;
			Plus=  In[1] & 0x0010 ? true : false ;
			Two =  In[2] & 0x0001 ? true : false ;
			One =  In[2] & 0x0002 ? true : false ;
			B   =  In[2] & 0x0004 ? true : false ;
			A   =  In[2] & 0x0008 ? true : false ;
			Minus= In[2] & 0x0010 ? true : false ;
			Home=  In[2] & 0x0080 ? true : false ;

			if (Home) {break;}
			written = 2;

			if (One) {
				WriteBuffer[0] = 0x13;
				WriteBuffer[1] = 0x01;
				WriteFile( w.hWiiRemoteHID, WriteBuffer ,w.Caps.OutputReportByteLength,&written,NULL);
				printf("[1]\n");
			} else {
				WriteBuffer[0] = 0x13;
				WriteBuffer[1] = 0x00;
				WriteFile( w.hWiiRemoteHID, WriteBuffer ,w.Caps.OutputReportByteLength,&written,NULL);
			}
		}
	} else {
		printf("Init failed");
	}
	w.CloseWiiRemoteHID( w.hWiiRemoteHID );
	return 0;
} 

